package us.wili.dev.client;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import us.wili.dev.client.filter.AlwaysCreateJwtTokenFilter;
import us.wili.dev.common.mybatis.IdAndTimeAutoUpdatePlugin;
import us.wili.dev.common.util.snowflake.SnowflakeUtil;


@SpringBootApplication(
        exclude = {SecurityAutoConfiguration.class},
        scanBasePackages = {"us.wili.dev"}
)
@MapperScan({"us.wili.dev.dao", "us.wili.dev.client.dao"})
public class ClientApplication {
    public static void main( String[] args ) {
        SpringApplication application = new SpringApplication(ClientApplication.class);
        application.run(args);
    }

    /**
     * Mybatis插件，用于生成ID、版本号、创建时间、更新时间
     * @param idWorker
     * @return
     */
    @Bean
    public IdAndTimeAutoUpdatePlugin idAndTimeAutoUpdatePlugin(SnowflakeUtil idWorker, SnowflakeUtil versionWorkder) throws Exception {
        return new IdAndTimeAutoUpdatePlugin(idWorker, versionWorkder);
    }

    /**
     * token 生成策略过滤器
     * @return
     */
    @Bean
    public AlwaysCreateJwtTokenFilter alwaysCreateJwtTokenFilter() {
        return new AlwaysCreateJwtTokenFilter();
    }
    @Bean
    public FilterRegistrationBean alwaysCreateJwtTokenFilterRegistrationBean(AlwaysCreateJwtTokenFilter alwaysCreateJwtTokenFilter) {
        FilterRegistrationBean registBean = new FilterRegistrationBean();
        registBean.setFilter(alwaysCreateJwtTokenFilter);
        registBean.addUrlPatterns("/*");
        //order的数值越小 则优先级越高
        registBean.setOrder(6);
        return registBean;
    }
}
